import {
  Controller,
  Get,
  Post,
  Delete,
  Patch,
  Request,
  Response,
  Param,
  HttpCode,
  Body,
  Query,
  Headers,
} from '@nestjs/common';
import { query } from 'express';

@Controller('/student')
export class StudentController {
  // 不同的装饰器，代表不同请求
  @Get('/findAll')
  findAll(@Request() req): string {
    console.log('IP address:', req.ip);
    console.log('Request path', req.path);
    console.log('HTTP methods:', req.method);
    console.log('Request headers:', req.headers);
    console.log('Query params:', req.query);
    return 'Handle Student GET查询 request';
  }

  @Post('/findAll')
  creat(): string {
    return 'Handle Student Post查询 request';
  }

  @Delete('/findAll')
  remove(): string {
    return 'Handle Student DELETE查询';
  }

  @Patch('/findAll')
  update(): string {
    return 'Handle Student Patch查询';
  }

  @Get('/findAll-json')
  findAllJson(@Request() req): any {
    console.log('Request object-------->', req.query);
    return {
      code: 200,
      message: '查询成功',
      list: [
        { id: '001', name: '张十三', age: 18, address: '湖南' },
        { id: '002', name: '丝绸', age: 22, address: '四川' },
      ],
    };
  }

  // body 注解接受 json 参数
  @Post('/addAll-json')
  creatJson(@Body() body): any {
    console.log(body);
    return {
      code: 200,
      message: '添加成功',
      list: [body],
    };
  }

  @Delete('/findAll-json')
  removeJson(): any {
    return {
      code: 200,
      message: '删除成功',
    };
  }

  @Patch('/findAll-json')
  updateJson(): any {
    return {
      code: 200,
      message: '修改成功',
    };
  }

  @Get('/findAll-response')
  findAllResponse(@Response() res): any {
    console.log('Response object-------->', res);
    res.status(200).send('Hello, World!');
  }

  @Get('/findAll-params/:id')
  @HttpCode(202)
  findAllParams(@Param() params): any {
    console.log(params);
    return {
      code: 200,
      message: params,
    };
  }

  @Get('/findAll-query')
  findQuery(
    @Query() query,
    @Headers() header,
    @Headers('authorzation') authorzation: string,
  ): any {
    console.log(query);
    console.log('请求头 ==>', header);
    console.log('authorzation ==>', authorzation);
    return {
      code: 200,
      message: query,
    };
  }
}
